<html>
<head><meta charset="utf-8"><title>Float to int conversions · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html">Float to int conversions</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="194961231"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194961231" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Mena Quintero <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194961231">(Apr 22 2020 at 17:03)</a>:</h4>
<p>I'm tickled that <a href="https://github.com/rust-lang/rust/pull/71269" title="https://github.com/rust-lang/rust/pull/71269">https://github.com/rust-lang/rust/pull/71269</a> is happening.  Are there plans for checked conversions, too?  Something like fn i32::checked_from(f64) -&gt; Result&lt;i32, DoesntFit&gt; ?</p>



<a name="194961764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194961764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194961764">(Apr 22 2020 at 17:07)</a>:</h4>
<p><span class="user-mention" data-user-id="219747">@Simon Sapin</span> had a pre-RFC for a bunch of such methods (I'm not sure if that particular one is in there).</p>



<a name="194962204"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194962204" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Mena Quintero <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194962204">(Apr 22 2020 at 17:10)</a>:</h4>
<p>ah, <a href="https://internals.rust-lang.org/t/pre-rfc-add-explicitly-named-numeric-conversion-apis/11395" title="https://internals.rust-lang.org/t/pre-rfc-add-explicitly-named-numeric-conversion-apis/11395">https://internals.rust-lang.org/t/pre-rfc-add-explicitly-named-numeric-conversion-apis/11395</a> ?</p>



<a name="194962306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194962306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194962306">(Apr 22 2020 at 17:10)</a>:</h4>
<p>I believe so yeah</p>



<a name="194962339"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194962339" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Mena Quintero <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194962339">(Apr 22 2020 at 17:11)</a>:</h4>
<p>excellent, thanks.  I'll subscribe to that.</p>



<a name="194962375"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194962375" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194962375">(Apr 22 2020 at 17:11)</a>:</h4>
<p>checked_from might be too simplistic (though could be a reasonable default). it's not very clear to me how rust will eventually support rounding modes etc in floating point well</p>



<a name="194962389"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194962389" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194962389">(Apr 22 2020 at 17:11)</a>:</h4>
<p>(or whether we even want to)</p>



<a name="194962584"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194962584" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Mena Quintero <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194962584">(Apr 22 2020 at 17:12)</a>:</h4>
<p>oh, hmm, that topic is closed now</p>



<a name="194962647"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194962647" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194962647">(Apr 22 2020 at 17:13)</a>:</h4>
<p>yeah it's an old pre-rfc, I think Simon ran out of time for it</p>



<a name="194962748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194962748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194962748">(Apr 22 2020 at 17:14)</a>:</h4>
<p>Yeah I still want to make a proper RFC, help gathering prior art would be appreciated</p>



<a name="194962793"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194962793" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Mena Quintero <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194962793">(Apr 22 2020 at 17:14)</a>:</h4>
<p>given the length of <a href="https://github.com/rust-lang/rust/issues/10184" title="https://github.com/rust-lang/rust/issues/10184">#10184</a>, that's perfectly understandable :)</p>



<a name="194962853"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194962853" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194962853">(Apr 22 2020 at 17:14)</a>:</h4>
<p><span class="user-mention" data-user-id="223500">@Federico Mena Quintero</span> Do you mean checking for overflow and NaN? Or should non-integer values also return an error instead of rounding?</p>



<a name="194962973"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194962973" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Mena Quintero <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194962973">(Apr 22 2020 at 17:15)</a>:</h4>
<p><span class="user-mention" data-user-id="219747">@Simon Sapin</span> so I just ran into a bug in librsvg where a big positive"f64 as i32" gives back a negative number, and in that case I don't want saturation; I want to know that it won't fit so I can do something else</p>



<a name="194963111"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194963111" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194963111">(Apr 22 2020 at 17:16)</a>:</h4>
<p>But fractional values should still be rounded/truncated?</p>



<a name="194963230"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194963230" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Mena Quintero <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194963230">(Apr 22 2020 at 17:17)</a>:</h4>
<p>I guess I want to check for overflow, as in not fitting within the int's range.  I'm not sure about NaNs; since this is a checked operation, they should probably also yield an error</p>



<a name="194963304"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194963304" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Mena Quintero <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194963304">(Apr 22 2020 at 17:17)</a>:</h4>
<p><span class="user-mention" data-user-id="219747">@Simon Sapin</span> yeah, round to zero is sensible for my case</p>



<a name="194963453"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194963453" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194963453">(Apr 22 2020 at 17:18)</a>:</h4>
<p>Ok, I think this is "Fallible approximate" that I mention in future possibilites</p>



<a name="194963463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194963463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Mena Quintero <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194963463">(Apr 22 2020 at 17:18)</a>:</h4>
<p>... I was looking at the docs, found the unstable approx_unchecked_to, got really happy that something there is happening already, and then went to look back at the issues/rfcs :)</p>



<a name="194963839"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194963839" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Mena Quintero <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194963839">(Apr 22 2020 at 17:21)</a>:</h4>
<p>ah, got it, indeed the "fallible approximate" case would cover this</p>



<a name="194964167"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194964167" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194964167">(Apr 22 2020 at 17:24)</a>:</h4>
<p>Unchecked is the one that has Undefined Behavior, so probably not what you want :)</p>



<a name="194964528"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194964528" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194964528">(Apr 22 2020 at 17:26)</a>:</h4>
<p>It’s what <code>as</code> does in current Rust versions. <code>as</code> will likely be changed to saturate which has extra runtime cost. That cost is typically low, but for the few cases where it isn’t (and the values are known not to overflow) we’re adding <code>unsafe fn to_int_unchecked</code> with the old behavior (which also matches C and C++ casts)</p>



<a name="194966618"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194966618" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Mena Quintero <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194966618">(Apr 22 2020 at 17:41)</a>:</h4>
<p>yeah, librsvg has a lot of <code>f64 as i32</code> stuff :(  Will start auditing it soon.  I think saturating casts could help that code in some cases; for some others I really want to know about overflow.  I can see that carefully-written pixel-crunching stuff would like the unchecked casts...</p>



<a name="194966795"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194966795" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Mena Quintero <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194966795">(Apr 22 2020 at 17:43)</a>:</h4>
<p>btw, let me thank you in passing for html5ever / kuchiki / all those.  They are super useful as crates and as examples of how to implement SVG lookalikes :)</p>



<a name="194988892"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194988892" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194988892">(Apr 22 2020 at 20:43)</a>:</h4>
<p><span class="user-mention silent" data-user-id="223500">Federico Mena Quintero</span> <a href="#narrow/stream/122651-general/topic/Float.20to.20int.20conversions/near/194962973" title="#narrow/stream/122651-general/topic/Float.20to.20int.20conversions/near/194962973">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="219747">Simon Sapin</span> so I just ran into a bug in librsvg where a big positive"f64 as i32" gives back a negative number, and in that case I don't want saturation; I want to know that it won't fit so I can do something else</p>
</blockquote>
<p>that sounds like you ran into UB, and cannot happen once <code>as</code> saturates (as in, once <a href="https://github.com/rust-lang/rust/pull/71269" title="https://github.com/rust-lang/rust/pull/71269">https://github.com/rust-lang/rust/pull/71269</a> lands)</p>



<a name="194989004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194989004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194989004">(Apr 22 2020 at 20:44)</a>:</h4>
<p>a positive float is then guaranteed to give a positive int</p>



<a name="194989028"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/194989028" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#194989028">(Apr 22 2020 at 20:44)</a>:</h4>
<p>(including pos. infinity, which returns <code>i32::MAX</code>)</p>



<a name="195000660"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195000660" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195000660">(Apr 22 2020 at 22:49)</a>:</h4>
<p><span class="user-mention" data-user-id="223500">@Federico Mena Quintero</span> you might also be interested in <a href="https://github.com/rust-lang/rfcs/pull/2484" title="https://github.com/rust-lang/rfcs/pull/2484">https://github.com/rust-lang/rfcs/pull/2484</a>, which hit the possibility explosion pretty hard</p>



<a name="195000773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195000773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Mena Quintero <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195000773">(Apr 22 2020 at 22:50)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> oh yeah, that was definitely UB and a bug.  A carry-over from the C code, unfortunately.</p>



<a name="195000991"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195000991" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Mena Quintero <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195000991">(Apr 22 2020 at 22:53)</a>:</h4>
<p><span class="user-mention" data-user-id="125270">@scottmcm</span> thanks, I'll read that</p>
<p>someone just suggested using the <code>cast</code> crate which sounds like exactly the low-impact solution we need right now</p>



<a name="195024251"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195024251" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195024251">(Apr 23 2020 at 07:01)</a>:</h4>
<p><span class="user-mention silent" data-user-id="223500">Federico Mena Quintero</span> <a href="#narrow/stream/122651-general/topic/Float.20to.20int.20conversions/near/195000773" title="#narrow/stream/122651-general/topic/Float.20to.20int.20conversions/near/195000773">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> oh yeah, that was definitely UB and a bug.  A carry-over from the C code, unfortunately.</p>
</blockquote>
<p>to be clear, this was/is a bug <em>in rustc</em>. we shouldn't have UB from safe <code>as</code> casts.</p>



<a name="195024323"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195024323" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195024323">(Apr 23 2020 at 07:02)</a>:</h4>
<p>So I think your problem is solved by <a href="https://github.com/rust-lang/rust/pull/71269" title="https://github.com/rust-lang/rust/pull/71269">https://github.com/rust-lang/rust/pull/71269</a>. is your question how to get a fix without waiting for that PR?</p>



<a name="195085621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195085621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Federico Mena Quintero <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195085621">(Apr 23 2020 at 16:23)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/122651-general/topic/Float.20to.20int.20conversions/near/195024323" title="#narrow/stream/122651-general/topic/Float.20to.20int.20conversions/near/195024323">said</a>:</p>
<blockquote>
<p>So I think your problem is solved by <a href="https://github.com/rust-lang/rust/pull/71269" title="https://github.com/rust-lang/rust/pull/71269">https://github.com/rust-lang/rust/pull/71269</a>. is your question how to get a fix without waiting for that PR?</p>
</blockquote>
<p>Oh, no, the code in librsvg <em>is</em> wrong even with saturating casts; it really needs to check if there's an overflow.  I'm happy that "as" is being finally un-UBed with saturation.  As for my problem I'll audit our "as" casts (lots of them, unfortunately, from the initial C to Rust conversion when I didn't know what I was doing) and replace them with something correct, I hope.</p>



<a name="195183166"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195183166" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195183166">(Apr 24 2020 at 12:55)</a>:</h4>
<p>I dont know what an "overflow" means when casting float-to-int</p>



<a name="195183187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195183187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195183187">(Apr 24 2020 at 12:55)</a>:</h4>
<p>but anyway it seems you have a plan forward so that's good :D</p>



<a name="195183373"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195183373" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195183373">(Apr 24 2020 at 12:56)</a>:</h4>
<p>maybe you mean <code>200.0 as i8</code>? but the result is 127, I wouldnt say it "overflows" -- in the linked issue we say that it "saturates". sure the cast loses information, but then so does <code>1.5 as i8</code>.</p>



<a name="195189048"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195189048" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195189048">(Apr 24 2020 at 13:41)</a>:</h4>
<p>I think they're asking for something like <code>fn checked_to_i32(self) -&gt; Option&lt;i32&gt;</code>, that never saturates but returns an error or <code>None</code>.</p>



<a name="195192028"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195192028" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195192028">(Apr 24 2020 at 14:04)</a>:</h4>
<p>and then someone asked above what it should do on <code>1.5</code>, and indeed I have the same question</p>



<a name="195192051"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195192051" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195192051">(Apr 24 2020 at 14:04)</a>:</h4>
<p>usually a "checked" cast should only succeed if the result is exactly representable</p>



<a name="195192083"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195192083" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195192083">(Apr 24 2020 at 14:04)</a>:</h4>
<p>for int-int, overflow is the only concern; for float-int we need to consider saturation <em>and</em> rounding</p>



<a name="195192545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195192545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195192545">(Apr 24 2020 at 14:08)</a>:</h4>
<p>"Whatever <code>1.5 as i32</code> does".</p>



<a name="195192620"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195192620" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195192620">(Apr 24 2020 at 14:08)</a>:</h4>
<p>It's implied that an <code>f32</code> won't be exactly representable as an <code>i32</code> most of the time. But we could have it under a different name, I guess. I <em>think</em> "exactly representable" can be tested with <code>x as i32 as f32 == x</code>, while "checked conversion without saturation" is harder to express.</p>



<a name="195194412"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195194412" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195194412">(Apr 24 2020 at 14:21)</a>:</h4>
<p>so... <code>127.99 as i8</code>, is that "rounding" or "saturation"?</p>



<a name="195194468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195194468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195194468">(Apr 24 2020 at 14:21)</a>:</h4>
<p>also I am somewhat curious in which cases rounding would be okay but saturation undesired, that seems awfully specific to me</p>



<a name="195194586"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195194586" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195194586">(Apr 24 2020 at 14:22)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/122651-general/topic/Float.20to.20int.20conversions/near/195194412" title="#narrow/stream/122651-general/topic/Float.20to.20int.20conversions/near/195194412">said</a>:</p>
<blockquote>
<p>so... <code>127.99 as i8</code>, is that "rounding" or "saturation"?</p>
</blockquote>
<p>answering myself: probably rounding. I guess the rule is "anything for which the unchecked cast has defined behavior returns <code>Some</code>; everything else returns <code>None</code>".</p>



<a name="195195303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195195303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195195303">(Apr 24 2020 at 14:27)</a>:</h4>
<p>Which is totally fine if you interpret <code>checked</code> as "like <code>unchecked</code> but tell me instead of triggering UB"</p>



<a name="195195370"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195195370" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195195370">(Apr 24 2020 at 14:27)</a>:</h4>
<p>right, what remains is my question for a usecase</p>



<a name="195195461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195195461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195195461">(Apr 24 2020 at 14:28)</a>:</h4>
<p>I can understand caring about information loss, but separating saturation from rounding seems really odd</p>



<a name="195195499"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195195499" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195195499">(Apr 24 2020 at 14:28)</a>:</h4>
<p>Maybe some audio processing code where you want to detect and warn in case of saturation (because it might be audible)</p>



<a name="195195528"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195195528" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195195528">(Apr 24 2020 at 14:28)</a>:</h4>
<p>the thing thats "unchecked" about the unchecked cast isn't motivated by some usecase, it's just how LLVM and C happen to work</p>



<a name="195195934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195195934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195195934">(Apr 24 2020 at 14:31)</a>:</h4>
<p>If I'm processing audio as float, then converting it back to integer, rounding is perfectly normal, as I'm quantizing to my target domain. Saturation is "okay" (better than UB, for sure), but it's still useful to know when it happens.</p>



<a name="195196161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195196161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195196161">(Apr 24 2020 at 14:32)</a>:</h4>
<p>I guess the workaround is to do the range check in float instead.</p>



<a name="195197064"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195197064" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195197064">(Apr 24 2020 at 14:38)</a>:</h4>
<p><span class="user-mention silent" data-user-id="203546">Laurențiu Nicola</span> <a href="#narrow/stream/122651-general/topic/Float.20to.20int.20conversions/near/195195934" title="#narrow/stream/122651-general/topic/Float.20to.20int.20conversions/near/195195934">said</a>:</p>
<blockquote>
<p>If I'm processing audio as float, then converting it back to integer, rounding is perfectly normal, as I'm quantizing to my target domain. Saturation is "okay" (better than UB, for sure), but it's still useful to know when it happens.</p>
</blockquote>
<p>sounds reasonable, but pretty niche</p>



<a name="195207312"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195207312" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Asa Zeren <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195207312">(Apr 24 2020 at 15:48)</a>:</h4>
<p>Perhaps a solution would be to have <code>checked</code> that will fail on saturation or rounding, and then something like <code>reporting</code> (bad name) that would return something like: </p>
<div class="codehilite"><pre><span></span><span class="k">struct</span> <span class="nc">FloatToIntReport</span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">result</span>: <span class="kt">i32</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">rounded</span>: <span class="kt">bool</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">saturated</span>: <span class="kt">bool</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="195444286"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Float%20to%20int%20conversions/near/195444286" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Float.20to.20int.20conversions.html#195444286">(Apr 27 2020 at 16:47)</a>:</h4>
<p>FWIW, the conversions in <code>num-traits</code> are properly checked for float to int</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>